package jiubeirobot.com.robotshop.ui.fragment.shop;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.base.BaseFragment;
import jiubeirobot.com.robotshop.bean.UserSubmitOrderPramsBean;
import jiubeirobot.com.robotshop.common.view.AddressSelectedView;
import jiubeirobot.com.robotshop.common.view.BoderButtton;
import jiubeirobot.com.robotshop.common.view.MyScrollView;
import jiubeirobot.com.robotshop.common.view.NoMoveEditTextView;
import jiubeirobot.com.robotshop.ui.activity.shop.CheckOrderActivity;
import jiubeirobot.com.robotshop.utils.TS;

/**
 * ==========================
 * 创建人：TheJoker丶豪 on
 * 创建时间：2017/9/4.
 * 创建内容：选择发票方式的fragment 界面
 * ==========================
 */

public class SelectInvoiceTypeFragment extends BaseFragment {
    @BindView(R.id.bbtn_no_invoice)
    BoderButtton bbtnNoInvoice;
    @BindView(R.id.bbtn_general_invoice)
    BoderButtton bbtnGeneralInvoice;
    @BindView(R.id.bbtn_add_val_invoice)
    BoderButtton bbtnAddValInvoice;
    @BindView(R.id.llyt_invoice_content)
    LinearLayout llytInvoiceContent;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.llyt_appreciation)
    LinearLayout llytAppreciation;
    @BindView(R.id.btn_submit_order)
    Button btnSubmitOrder;
    @BindView(R.id.rb_geren)
    RadioButton rbPerson;
    @BindView(R.id.et_general_name)
    EditText etGeneralName;
//    @BindView(R.id.rb_invoiceContent_person)
//    RadioButton rbInvoiceContentPerson;
//    @BindView(R.id.rg_invoiceContent)
//    RadioGroup rgInvoiceContent;

    @BindView(R.id.msv_scroll)
    MyScrollView msvScroll;
    @BindView(R.id.ll_new)
    LinearLayout llytNomal;
    @BindView(R.id.rb_danwei)
    RadioButton rbDanwei;
    @BindView(R.id.rg_fapiao)
    RadioGroup rg_fapiao;
    @BindView(R.id.et_nomal_tel)
    EditText etTel;
    @BindView(R.id.et_nomal_bank)
    EditText et_nomal_bank;
    @BindView(R.id.et_nomal_account)
    EditText et_nomal_account;

    private int type;//发票类型
    private String province, city, district, addId;
    //    普通发票
    //纳税号码
    @BindView(R.id.et_nomal_code)
    EditText etNomalcode;
    //电话
    @BindView(R.id.et_nomal_name)
    EditText et_nomal_name;
    /**
     * 增值税
     */
    @BindView(R.id.et_appreciation_name)
    EditText et_appreciation_name;
    @BindView(R.id.et_ratepayer_code)
    EditText et_ratepayer_code;
    @BindView(R.id.et_company_reg_address)
    NoMoveEditTextView et_company_reg_address;
    @BindView(R.id.et_company_tel)
    EditText et_company_tel;
    @BindView(R.id.et_deposit_bank)
    EditText et_deposit_bank;
    @BindView(R.id.et_deposit_account)
    EditText et_deposit_account;
    @BindView(R.id.et_invoice_name)
    EditText et_invoice_name;
    @BindView(R.id.tv_addres)
    TextView tv_addres;
    @BindView(R.id.et_invoice_tel)
    EditText et_invoice_tel;
    @BindView(R.id.llyt_address)
    AddressSelectedView llytAddress;
    @BindView(R.id.et_invoice_address)
    EditText etInvoiceAddress;

    CheckOrderActivity checkOrderActivity;

    @Override
    public int pageLayout() {
        return R.layout.fragment_select_invoice_type;
    }

    @Override
    public void initTitle() {
        checkOrderActivity = (CheckOrderActivity) getActivity();
    }

    @Override
    public void initView() {

        bbtnNoInvoice.setText(getString(R.string.shopping_check_no_invoice));
        bbtnGeneralInvoice.setText(getString(R.string.shopping_check_general_invoice));
        bbtnAddValInvoice.setText(getString(R.string.shopping_check_add_val_invoice));
        bbtnNoInvoice.setChecked(true);

        bbtnNoInvoice.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectInvoiceType(0);
            }
        });
        bbtnGeneralInvoice.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectInvoiceType(1);
            }
        });
        bbtnAddValInvoice.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectInvoiceType(2);
            }
        });

        llytAddress.setCancelClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llytAddress.setVisibility(View.GONE);
            }
        });
        llytAddress.setOptionsSelectListener(new AddressSelectedView.OptionsSelectListener() {
            @Override
            public void onOptionsSelect(String province, String city, String bean, String id, View v) {
                tv_addres.setText(province + " " + city + " " + bean);
                llytAddress.setVisibility(View.GONE);
                SelectInvoiceTypeFragment.this.addId = id;
            }
        });

        et_company_reg_address.setScrollView(msvScroll);

        msvScroll.setLlytAddress(llytAddress);

    }

    @Override
    public void onSuccess(BaseBean bean, int flag) {

    }

    private void selectInvoiceType(int type) {
        switch (type) {
            case 0:
                this.type = 1;
                bbtnNoInvoice.setChecked(true);
                bbtnGeneralInvoice.setChecked(false);
                bbtnAddValInvoice.setChecked(false);
                llytInvoiceContent.setVisibility(View.GONE);
                llytAppreciation.setVisibility(View.GONE);
                break;
            case 1:
                this.type = 2;
                bbtnNoInvoice.setChecked(false);
                bbtnGeneralInvoice.setChecked(true);
                bbtnAddValInvoice.setChecked(false);
                llytInvoiceContent.setVisibility(View.VISIBLE);
                llytAppreciation.setVisibility(View.GONE);

                rg_fapiao.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId) {
                            case R.id.rb_danwei:
                                etGeneralName.setVisibility(View.GONE);
                                llytNomal.setVisibility(View.VISIBLE);
                                break;
                            case R.id.rb_geren:
                                etGeneralName.setVisibility(View.VISIBLE);
                                llytNomal.setVisibility(View.GONE);
                                break;
                        }
                    }
                });

                break;
            case 2:
                this.type = 3;
                bbtnNoInvoice.setChecked(false);
                bbtnGeneralInvoice.setChecked(false);
                bbtnAddValInvoice.setChecked(true);
                llytAppreciation.setVisibility(View.VISIBLE);
                llytInvoiceContent.setVisibility(View.GONE);
                break;
        }
    }


    @OnClick({R.id.iv_close, R.id.tv_addres, R.id.btn_submit_order})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                if (checkOrderActivity != null) {
                    checkOrderActivity.close();
                }

                break;
            case R.id.tv_addres:
                if (llytAddress.getVisibility() == View.VISIBLE) {
                    llytAddress.setVisibility(View.GONE);
                } else {
                    llytAddress.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.btn_submit_order:
                if (!checkPage()) {
                    return;
                }
                if (checkOrderActivity != null) {
                    if (checkOrderActivity != null) {
                        UserSubmitOrderPramsBean pramsBean = new UserSubmitOrderPramsBean();
                        pramsBean.setOrder_type("android");
                        pramsBean.setType(type + "");
                        if (type == 2) {
                            pramsBean.setType(type + "");
                            pramsBean.setOrder_type("android");
                            pramsBean.setInvoiceType(rbPerson.isChecked() ? getString(R.string.common_personage) : getString(R.string.shopping_check_invoice_catering));
                            if (rbPerson.isChecked()) {
                                pramsBean.setInvoice_true_name(etGeneralName.getText() + "");
                            } else {
                                pramsBean.setInvoice_company_name(et_nomal_name.getText() + "");//公司名
                                pramsBean.setInvoice_taxpayer_code(etNomalcode.getText() + "");//纳税号码
                                pramsBean.setInvoice_register_phone(etTel.getText() + "");//电话号码
                                pramsBean.setInvoice_register_bank_name(et_nomal_name.getText() + "");//开户银行
                                pramsBean.setInvoice_register_bank_account(et_nomal_account.getText() + "");//银行账号
                            }

                        } else if (type == 3) {
                            pramsBean.setOrder_type("android");
                            pramsBean.setInvoice_company_name(et_appreciation_name.getText() + "");//公司名
                            pramsBean.setInvoice_taxpayer_code(et_ratepayer_code.getText() + "");//纳税号码
                            pramsBean.setInvoice_register_phone(et_company_tel.getText() + "");//电话号码
                            pramsBean.setInvoice_register_bank_name(et_deposit_bank.getText() + "");//开户银行
                            pramsBean.setInvoice_register_bank_account(et_deposit_account.getText() + "");//银行账号
                            pramsBean.setInvoice_register_address(et_company_reg_address.getText() + "");//地址
                            pramsBean.setInvoice_true_name(et_invoice_name.getText() + "");
                            pramsBean.setInvoice_phone(et_invoice_tel.getText() + "");
                            pramsBean.setInvoice_address(addId);
                            pramsBean.setInvoice_address_details(etInvoiceAddress.getText() + "");
                            pramsBean.setType(type + "");

                        }
                        checkOrderActivity.setInvoiceType(pramsBean);
                    }
                    checkOrderActivity.close();
                }
        }
    }

    private boolean checkPage() {
        if (type == 2) {
            if (rbPerson.isChecked()) {
                if (TextUtils.isEmpty(etGeneralName.getText())) {
                    TS.showToCenter(getString(R.string.shopping_check_invoice_person_hint));
                    return false;
                }
            }
            if (rbDanwei.isChecked()) {
                if (TextUtils.isEmpty(et_nomal_name.getText())) {
                    TS.showToCenter(getString(R.string.shopping_check_unit_name_hint));
                    return false;
                }
                if (TextUtils.isEmpty(etNomalcode.getText())) {
                    TS.showToCenter(getString(R.string.shopping_check_pay_taxes));
                    return false;
                }
            }
        } else if (type == 3) {
            if (TextUtils.isEmpty(et_appreciation_name.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_unit_name_hint));
                return false;
            }
            if (TextUtils.isEmpty(et_ratepayer_code.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_pay_taxes));
                return false;
            }
            if (TextUtils.isEmpty(et_company_reg_address.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_register_address));
                return false;
            }
            if (TextUtils.isEmpty(et_company_tel.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_tiket_phone));
                return false;
            }
            if (TextUtils.isEmpty(et_deposit_bank.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_account_bank));
                return false;
            }
            if (TextUtils.isEmpty(et_deposit_account.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_bank_account));
                return false;
            }
            if (TextUtils.isEmpty(et_invoice_name.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_tiket_name));
                return false;
            }
            if (TextUtils.isEmpty(tv_addres.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_tiket_address));
                return false;
            }
            if (TextUtils.isEmpty(etInvoiceAddress.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_tiket_details_address));
                return false;
            }
            if (TextUtils.isEmpty(et_invoice_tel.getText())) {
                TS.showToCenter(getString(R.string.shopping_check_tiket_phone));
                return false;
            }
        }
        return true;
    }
}
